package com.zzyk.main.service.impl;

import com.zzyk.main.mapper.DeviceOriginDataMapper;
import com.zzyk.main.mapper.MedicalReportMonthMapper;
import com.zzyk.main.model.pojo.MedicalReportMonth;
import com.zzyk.main.service.DeviceOriginDataService;
import com.zzyk.main.service.MedicalReportMonthService;
import com.zzyk.main.utils.MathUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MedicalReportMonthServiceImpl implements MedicalReportMonthService {

    @Resource
    private MedicalReportMonthMapper medicalReportMonthMapper;
    @Resource
    private DeviceOriginDataService deviceOriginDataService;
    @Resource
    private DeviceOriginDataMapper dataMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return medicalReportMonthMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(MedicalReportMonth record) {
        return medicalReportMonthMapper.insert(record);
    }

    @Override
    public int insertSelective(MedicalReportMonth record) {
        return medicalReportMonthMapper.insertSelective(record);
    }

    @Override
    public MedicalReportMonth selectByPrimaryKey(Integer id) {
        return medicalReportMonthMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(MedicalReportMonth record) {
        return medicalReportMonthMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MedicalReportMonth record) {
        return medicalReportMonthMapper.updateByPrimaryKey(record);
    }

    @Override
    public MedicalReportMonth findMedicalReportByMonth(String month, Integer enterprise, Integer[] deviceList) {
        MedicalReportMonth medicalReportMonth = dataMapper.selectMedicalReportMonth(month, enterprise, deviceList);
        double zongfen = 0;//总体评分
        String zongsion = "";//总结论
        Double counts = Double.parseDouble(medicalReportMonth.getCounts());//总次数
        if (counts == 0) {
            return null;
        }
        //合格率=（总数-超限次数）/总数
        Integer overrunA = medicalReportMonth.getOverrunAVoltage();//A相电压超限次数
        double passrateA = MathUtil.divide(Double.valueOf(Math.round((counts - overrunA) / counts * 100 * 10000)), 10000.0, 4);//A相电压合格率
        Integer overrunB = medicalReportMonth.getOverrunBVoltage();//B相电压超限次数
        double passrateB = MathUtil.divide(Double.valueOf(Math.round((counts - overrunB) / counts * 100 * 10000)), 10000.0, 4);//B相电压合格率
        Integer overrunC = medicalReportMonth.getOverrunCVoltage();//C相电压超限次数
        double passrateC = MathUtil.divide(Double.valueOf(Math.round((counts - overrunC) / counts * 100 * 10000)), 10000.0, 4);//C相电压合格率
        String ABCsion = "";//电压质量分析结论
        if (passrateA >= 60 && passrateB >= 60 && passrateC >= 60) {
            ABCsion = "合格";
            zongsion += "三相电压合格,";
        } else {
            ABCsion = "不合格";
            zongsion += "三相电压不合格,";
        }
        medicalReportMonth.setPassRateAVoltage(passrateA);
        medicalReportMonth.setPassRateBVoltage(passrateB);
        medicalReportMonth.setPassRateCVoltage(passrateC);
        medicalReportMonth.setConclusionVoltage(ABCsion);
        String passPowerFactor = medicalReportMonth.getOverrunHz();//频率质量分析超限次数
        double passrateHZ = MathUtil.divide(Double.valueOf(Math.round((counts - Double.parseDouble(passPowerFactor)) / counts * 100 * 10000)), 10000.0, 4);//频率质量分析合格率
        String HZsion = "";//频率质量分析结论
        if (passrateHZ >= 60) {
            HZsion = "合格";
            zongsion += "频率质量合格,";
        } else {
            HZsion = "不合格";
            zongsion += "频率质量不合格,";
        }
        medicalReportMonth.setConclusionHz(HZsion);
        medicalReportMonth.setPassRateHz(passrateHZ);
        String overrunU = medicalReportMonth.getOverrunUnbalanced();//三相不平衡超限次数  低于60分超限，不合格
        double passrateU = MathUtil.divide(Double.valueOf(Math.round((counts - Double.parseDouble(overrunU)) / counts * 100 * 10000)), 10000.0, 4);//三相不平衡合格率
        String Usion = "";//功率因数结论
        if (passrateU >= 60) {
            Usion = "合格";
            zongsion += "三相不平衡合格,";
        } else {
            Usion = "不合格";
            zongsion += "三相不平衡不合格,";
        }
        medicalReportMonth.setConclusionUnbalanced(Usion);
        medicalReportMonth.setPassRateUnbalanced(passrateU);
        String pFdoub = String.valueOf(medicalReportMonth.getPowerFactorLevel());
        double pF = MathUtil.divide(Double.valueOf(Math.round(Double.parseDouble(pFdoub) / 100.0 * 10000)), 10000.0, 4);
        String powerFa = String.valueOf(medicalReportMonth.getPassPowerFactor());//
        double powerFactor = MathUtil.divide(Double.valueOf(Math.round((counts - Double.parseDouble(powerFa)) / counts * 100 * 10000)), 10000.0, 4);//功率因数水平 低于70分超限，不合格
        String pFsion = "";//功率因数结论
        if (powerFactor >= 60) {
            pFsion = "合格";
            zongsion += "功率因数合格。";
        } else {
            pFsion = "不合格";
            zongsion += "功率因数不合格。";
        }
        zongfen = MathUtil.divide(Double.valueOf(Math.round((passrateA + passrateB + passrateC + passrateHZ + passrateU + powerFactor) / 6.0 * 10000)), 10000.0, 4);
        medicalReportMonth.setPassPowerFactor(pF);
        medicalReportMonth.setPowerFactorConclusion(pFsion);
        medicalReportMonth.setOverallScore(zongfen);
        medicalReportMonth.setConclusion(zongsion);
        return medicalReportMonth;
    }

}



